{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from tqdm import tqdm\n",
    "from datetime import datetime\n",
    "import os\n",
    "from joblib import Parallel, delayed"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "route_order_folder_path = '../data/route_order_data'\n",
    "route_order_first_wash='../data/firstwash'\n",
    "test_data_path = '../data/A_testData0531.csv'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['CNYTN-MXZLO', 'CNSHK-MYTPP', 'CNSHK-SGSIN', 'CNSHK-CLVAP',\n",
       "       'CNYTN-ARENA', 'CNYTN-MATNG', 'CNSHK-GRPIR', 'CNSHK-PKQCT',\n",
       "       'COBUN-HKHKG', 'CNYTN-PAONX', 'CNSHK-SIKOP', 'CNYTN-CAVAN',\n",
       "       'CNSHK-ESALG', 'CNYTN-MTMLA', 'CNSHK-ZADUR', 'CNSHK-LBBEY',\n",
       "       'CNSHA-SGSIN', 'CNYTN-RTM', 'CNHKG-MXZLO', 'HKHKG-FRFOS',\n",
       "       'CNYTN-NZAKL', 'CNSHA-PAMIT'], dtype=object)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#读唯一路由\n",
    "def get_test_route_set(path):\n",
    "    data = pd.read_csv(path) \n",
    "    test_route_set = data['TRANSPORT_TRACE'].unique()\n",
    "    return test_route_set\n",
    "test_route_set = get_test_route_set(test_data_path)\n",
    "test_route_set\n",
    "#读港口信息，测试路由用到的港口"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "from math import radians, cos, sin, asin, sqrt\n",
    "def haversine(lon1, lat1, lon2, lat2): # 经度1，纬度1，经度2，纬度2 （十进制度数）\n",
    "    # 将十进制度数转化为弧度\n",
    "    lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])\n",
    "    # haversine公式\n",
    "    dlon = lon2 - lon1 \n",
    "    dlat = lat2 - lat1 \n",
    "    a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2\n",
    "    c = 2 * asin(sqrt(a)) \n",
    "    r = 6371 # 地球平均半径，单位为公里\n",
    "    return c * r * 1000\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "order_route_a_data=pd.read_csv(route_order_folder_path+'/CNYTN-RTM.csv'\n",
    "                              , names=['loadingOrder', 'carrierName','timestamp','longitude','latitude','vesselMMSI','speed','direction','TRANSPORT_TRACE'])\n",
    "order_route_a_data.insert(6,'speed_count',0)\n",
    "order_len=len(order_route_a_data)\n",
    "order_route_a_data['number']=range(len(order_route_a_data))#创建索引\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>loadingOrder</th>\n",
       "      <th>carrierName</th>\n",
       "      <th>timestamp</th>\n",
       "      <th>longitude</th>\n",
       "      <th>latitude</th>\n",
       "      <th>vesselMMSI</th>\n",
       "      <th>speed_count</th>\n",
       "      <th>speed</th>\n",
       "      <th>direction</th>\n",
       "      <th>TRANSPORT_TRACE</th>\n",
       "      <th>number</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>NX349512137942</td>\n",
       "      <td>2019-01-12T23:19:03.000Z</td>\n",
       "      <td>114.277778</td>\n",
       "      <td>22.579038</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>NX349512137942</td>\n",
       "      <td>2019-01-12T23:19:03.000Z</td>\n",
       "      <td>114.277778</td>\n",
       "      <td>22.579038</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>NX349512137942</td>\n",
       "      <td>2019-01-12T23:52:08.000Z</td>\n",
       "      <td>114.277698</td>\n",
       "      <td>22.578882</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>NX349512137942</td>\n",
       "      <td>2019-01-12T23:52:08.000Z</td>\n",
       "      <td>114.277698</td>\n",
       "      <td>22.578882</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>NX349512137942</td>\n",
       "      <td>2019-01-13T01:58:00.000Z</td>\n",
       "      <td>114.277890</td>\n",
       "      <td>22.579057</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>242818</th>\n",
       "      <td>HF183094870010</td>\n",
       "      <td>2020-04-26T16:41:52.000Z</td>\n",
       "      <td>-6.193362</td>\n",
       "      <td>53.343490</td>\n",
       "      <td>8</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>242818</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>242819</th>\n",
       "      <td>HF183094870010</td>\n",
       "      <td>2020-04-26T16:46:02.000Z</td>\n",
       "      <td>-6.200863</td>\n",
       "      <td>53.343467</td>\n",
       "      <td>6</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>242819</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>242820</th>\n",
       "      <td>HF183094870010</td>\n",
       "      <td>2020-04-26T16:49:33.000Z</td>\n",
       "      <td>-6.205698</td>\n",
       "      <td>53.343420</td>\n",
       "      <td>4</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>242820</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>242821</th>\n",
       "      <td>HF183094870010</td>\n",
       "      <td>2020-04-26T16:51:42.000Z</td>\n",
       "      <td>-6.207482</td>\n",
       "      <td>53.343335</td>\n",
       "      <td>2</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>242821</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>242822</th>\n",
       "      <td>HF183094870010</td>\n",
       "      <td>2020-04-26T16:53:32.000Z</td>\n",
       "      <td>-6.207982</td>\n",
       "      <td>53.343322</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>242822</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>242823 rows × 11 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "          loadingOrder               carrierName   timestamp  longitude  \\\n",
       "0       NX349512137942  2019-01-12T23:19:03.000Z  114.277778  22.579038   \n",
       "1       NX349512137942  2019-01-12T23:19:03.000Z  114.277778  22.579038   \n",
       "2       NX349512137942  2019-01-12T23:52:08.000Z  114.277698  22.578882   \n",
       "3       NX349512137942  2019-01-12T23:52:08.000Z  114.277698  22.578882   \n",
       "4       NX349512137942  2019-01-13T01:58:00.000Z  114.277890  22.579057   \n",
       "...                ...                       ...         ...        ...   \n",
       "242818  HF183094870010  2020-04-26T16:41:52.000Z   -6.193362  53.343490   \n",
       "242819  HF183094870010  2020-04-26T16:46:02.000Z   -6.200863  53.343467   \n",
       "242820  HF183094870010  2020-04-26T16:49:33.000Z   -6.205698  53.343420   \n",
       "242821  HF183094870010  2020-04-26T16:51:42.000Z   -6.207482  53.343335   \n",
       "242822  HF183094870010  2020-04-26T16:53:32.000Z   -6.207982  53.343322   \n",
       "\n",
       "        latitude  vesselMMSI  speed_count  speed  direction  TRANSPORT_TRACE  \\\n",
       "0              0         NaN            0    NaN        NaN              NaN   \n",
       "1              0         NaN            0    NaN        NaN              NaN   \n",
       "2              0         NaN            0    NaN        NaN              NaN   \n",
       "3              0         NaN            0    NaN        NaN              NaN   \n",
       "4              0         NaN            0    NaN        NaN              NaN   \n",
       "...          ...         ...          ...    ...        ...              ...   \n",
       "242818         8         NaN            0    NaN        NaN              NaN   \n",
       "242819         6         NaN            0    NaN        NaN              NaN   \n",
       "242820         4         NaN            0    NaN        NaN              NaN   \n",
       "242821         2         NaN            0    NaN        NaN              NaN   \n",
       "242822         0         NaN            0    NaN        NaN              NaN   \n",
       "\n",
       "        number  \n",
       "0            0  \n",
       "1            1  \n",
       "2            2  \n",
       "3            3  \n",
       "4            4  \n",
       "...        ...  \n",
       "242818  242818  \n",
       "242819  242819  \n",
       "242820  242820  \n",
       "242821  242821  \n",
       "242822  242822  \n",
       "\n",
       "[242823 rows x 11 columns]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "order_route_a_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 24/24 [00:02<00:00,  8.71it/s]\n",
      "D:\\Program\\Anaconda\\envs\\AI\\lib\\site-packages\\joblib\\disk.py:122: UserWarning: Unable to delete folder C:\\Users\\zhang\\AppData\\Local\\Temp\\joblib_memmapping_folder_13068_1764177213 after 5 tentatives.\n",
      "  .format(folder_path, RM_SUBDIRS_N_RETRY))\n"
     ]
    },
    {
     "ename": "PermissionError",
     "evalue": "[WinError 32] 另一个程序正在使用此文件，进程无法访问。: 'C:\\\\Users\\\\zhang\\\\AppData\\\\Local\\\\Temp\\\\joblib_memmapping_folder_13068_1764177213\\\\13068-3089723526856-075db62c52754b22a8ff1a0df589b230.pkl'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mPermissionError\u001b[0m                           Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-7-943c2bca5be8>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     35\u001b[0m result = Parallel(n_jobs=8)(delayed(order_wash)\n\u001b[0;32m     36\u001b[0m                                     \u001b[1;33m(\u001b[0m\u001b[0morder\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mgps_information\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 37\u001b[1;33m                                     for order,gps_information in tqdm(a_order_route_a_data))\n\u001b[0m\u001b[0;32m     38\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     39\u001b[0m \u001b[0mresult\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\Program\\Anaconda\\envs\\AI\\lib\\site-packages\\joblib\\parallel.py\u001b[0m in \u001b[0;36m__call__\u001b[1;34m(self, iterable)\u001b[0m\n\u001b[0;32m   1025\u001b[0m                 \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_backend\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mstop_call\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1026\u001b[0m             \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_managed_backend\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1027\u001b[1;33m                 \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_terminate_backend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1028\u001b[0m             \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_jobs\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mlist\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1029\u001b[0m             \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_pickle_cache\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\Program\\Anaconda\\envs\\AI\\lib\\site-packages\\joblib\\parallel.py\u001b[0m in \u001b[0;36m_terminate_backend\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    732\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0m_terminate_backend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    733\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_backend\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 734\u001b[1;33m             \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_backend\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mterminate\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    735\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    736\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0m_dispatch\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mbatch\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\Program\\Anaconda\\envs\\AI\\lib\\site-packages\\joblib\\_parallel_backends.py\u001b[0m in \u001b[0;36mterminate\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    569\u001b[0m             \u001b[1;31m# in latter calls but we free as much memory as we can by deleting\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    570\u001b[0m             \u001b[1;31m# the shared memory\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 571\u001b[1;33m             \u001b[0mdelete_folder\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_workers\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_temp_folder\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    572\u001b[0m             \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_workers\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    573\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\Program\\Anaconda\\envs\\AI\\lib\\site-packages\\joblib\\disk.py\u001b[0m in \u001b[0;36mdelete_folder\u001b[1;34m(folder_path, onerror)\u001b[0m\n\u001b[0;32m    113\u001b[0m             \u001b[1;32mwhile\u001b[0m \u001b[1;32mTrue\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    114\u001b[0m                 \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 115\u001b[1;33m                     \u001b[0mshutil\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrmtree\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfolder_path\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;32mFalse\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    116\u001b[0m                     \u001b[1;32mbreak\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    117\u001b[0m                 \u001b[1;32mexcept\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mOSError\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mWindowsError\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\Program\\Anaconda\\envs\\AI\\lib\\shutil.py\u001b[0m in \u001b[0;36mrmtree\u001b[1;34m(path, ignore_errors, onerror)\u001b[0m\n\u001b[0;32m    514\u001b[0m             \u001b[1;31m# can't continue even if onerror hook returns\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    515\u001b[0m             \u001b[1;32mreturn\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 516\u001b[1;33m         \u001b[1;32mreturn\u001b[0m \u001b[0m_rmtree_unsafe\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpath\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0monerror\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    517\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    518\u001b[0m \u001b[1;31m# Allow introspection of whether or not the hardening against symlink\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\Program\\Anaconda\\envs\\AI\\lib\\shutil.py\u001b[0m in \u001b[0;36m_rmtree_unsafe\u001b[1;34m(path, onerror)\u001b[0m\n\u001b[0;32m    398\u001b[0m                 \u001b[0mos\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0munlink\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfullname\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    399\u001b[0m             \u001b[1;32mexcept\u001b[0m \u001b[0mOSError\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 400\u001b[1;33m                 \u001b[0monerror\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mos\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0munlink\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfullname\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msys\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mexc_info\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    401\u001b[0m     \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    402\u001b[0m         \u001b[0mos\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrmdir\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpath\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\Program\\Anaconda\\envs\\AI\\lib\\shutil.py\u001b[0m in \u001b[0;36m_rmtree_unsafe\u001b[1;34m(path, onerror)\u001b[0m\n\u001b[0;32m    396\u001b[0m         \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    397\u001b[0m             \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 398\u001b[1;33m                 \u001b[0mos\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0munlink\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfullname\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    399\u001b[0m             \u001b[1;32mexcept\u001b[0m \u001b[0mOSError\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    400\u001b[0m                 \u001b[0monerror\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mos\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0munlink\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfullname\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msys\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mexc_info\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mPermissionError\u001b[0m: [WinError 32] 另一个程序正在使用此文件，进程无法访问。: 'C:\\\\Users\\\\zhang\\\\AppData\\\\Local\\\\Temp\\\\joblib_memmapping_folder_13068_1764177213\\\\13068-3089723526856-075db62c52754b22a8ff1a0df589b230.pkl'"
     ]
    }
   ],
   "source": [
    "#按照订单分组\n",
    "order_route_a_data['timestamp'] = pd.to_datetime(order_route_a_data['timestamp'], infer_datetime_format=True)\n",
    "a_order_route_a_data=order_route_a_data.groupby('loadingOrder')\n",
    "front_lon=0\n",
    "front_lat=0\n",
    "front_timestamp=datetime.now()\n",
    "\n",
    "a = 0\n",
    "\n",
    "def order_wash(order,gps_information):\n",
    "    drop_list = []\n",
    "#     row_number=gps_information.shape[0]#row number\n",
    "#     count=row_number\n",
    "#     for date, row in gps_information.iterrows(): \n",
    "#         now_lon=row.longitude\n",
    "#         now_lat=row.latitude\n",
    "#         now_timestamp=row.timestamp\n",
    "#         if count!=row_number:\n",
    "#             time=(now_timestamp-front_timestamp).seconds#by seconds\n",
    "#             if time==0:\n",
    "#                 time=1\n",
    "#             distance=haversine(front_lon, front_lat, now_lon, now_lat)#by kilometer\n",
    "#             row.speed_count=int(distance*3.6/time)\n",
    "\n",
    "#             if (abs(row.speed_count-row.speed))>8:\n",
    "#                 drop_list = drop_list.append(row.number)\n",
    "# #                 order_route_a_data.drop(row.number,axis=0,inplace=True)\n",
    "#         front_lon=now_lon\n",
    "#         front_lat=now_lat\n",
    "#         front_timestamp=now_timestamp\n",
    "#         count-=1\n",
    "    drop_list.append(gps_information)\n",
    "    return drop_list\n",
    "\n",
    "result = Parallel(n_jobs=8)(delayed(order_wash)\n",
    "                                    (order,gps_information)\n",
    "                                    for order,gps_information in tqdm(a_order_route_a_data))\n",
    "\n",
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''        \n",
    "for order,gps_information in tqdm(a_order_route_a_data):  \n",
    "    \n",
    "    row_number=gps_information.shape[0]#row number\n",
    "    count=row_number\n",
    "    for date, row in gps_information.iterrows(): \n",
    "        now_lon=row.longitude\n",
    "        now_lat=row.latitude\n",
    "        now_timestamp=row.timestamp\n",
    "        if count!=row_number:\n",
    "            time=(now_timestamp-front_timestamp).seconds#by seconds\n",
    "            if time==0:\n",
    "                time=1\n",
    "            distance=haversine(front_lon, front_lat, now_lon, now_lat)#by kilometer\n",
    "            row.speed_count=int(distance*3.6/time)\n",
    "            #print( row.loadingOrder,row.timestamp,row.speed_count ,row.speed ,row.speed_count-row.speed)\n",
    "            if (row.speed_count-row.speed)>8:\n",
    "                try:\n",
    "                    order_route_a_data.drop(row.number,axis=0,inplace=True)\n",
    "                except Exception as err:\n",
    "                    print(row)\n",
    "                #print(row)\n",
    "        front_lon=now_lon\n",
    "        front_lat=now_lat\n",
    "        front_timestamp=now_timestamp\n",
    "        count-=1\n",
    "'''\n",
    "# order_route_a_data.drop(['number','speed_count'],axis=1,inplace=True)\n",
    "# order_route_a_data.head(100)\n",
    "# order_route_a_data.to_csv(route_order_first_wash+'/CNYTN-RTM.csv',header=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "order_route_a_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.7.6 64-bit ('AI': conda)",
   "language": "python",
   "name": "python37664bitaiconda6859e03b37c34f0182c9bde8073269f7"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
